package com.ele.service.impl;

import com.ele.dao.FoodCategoryDao;
import com.ele.dao.ShopDao;
import com.ele.dao.impl.FoodCategoryDaoImpl;
import com.ele.dao.impl.ShopDaoImpl;
import com.ele.pojo.FoodCategory;
import com.ele.pojo.PageResult;
import com.ele.pojo.Shop;
import com.ele.pojo.vo.FoodCategoryVO;
import com.ele.service.FoodCategoryService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodCategoryServiceImpl implements FoodCategoryService {
    private FoodCategoryDao foodCategoryDao = new FoodCategoryDaoImpl();
    private ShopDao shopDao = new ShopDaoImpl();

    public Integer foodCategoryAdd(String categoryName, String shopId) {
        try{
            return foodCategoryDao.foodCategoryAdd(categoryName,shopId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    public Integer foodCategoryUpdate(String categoryName, String categoryId)  {
        try{
            return this.foodCategoryDao.foodCategoryUpdate(categoryName,categoryId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    public Integer foodCategoryDelete(String categoryId)  {
        try{
            return this.foodCategoryDao.foodCategoryDelete(categoryId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    public PageResult<List<FoodCategoryVO>> findFoodCategory(String currentPage, String pageSize, String shopId)  {
        try {
            // 查询到只有菜品分类的数据，其中不 包含 菜品所属的店铺名称等信息
            List<FoodCategory> list = this.foodCategoryDao.findFoodCategory(currentPage, pageSize, shopId);
            List<FoodCategoryVO> foodCategoryVO = new ArrayList<>();
            // 遍历当前所有菜品分类的集合
            for (FoodCategory foodCategory : list) {
                FoodCategoryVO vo = new FoodCategoryVO();
                Integer sId = foodCategory.getShopId();
                // 通过店铺的id去查询店铺的信息，需要获取到店铺的名称
                Shop shop = shopDao.findShopById(sId);
                // 封装数据
                vo.setShopName(shop.getShopName());
                vo.setCategoryId(foodCategory.getCategoryId());
                vo.setCategoryName(foodCategory.getCategoryName());
                vo.setShopId(foodCategory.getShopId());

                // 将封装好的vo对象添加集合中
                foodCategoryVO.add(vo);
            }
            // 查询总记录数
            Long count = this.foodCategoryDao.foodCategoryCount(shopId);
            PageResult<List<FoodCategoryVO>> pageResult = new PageResult<List<FoodCategoryVO>>();
            pageResult.setTotal(count);
            pageResult.setDatas(foodCategoryVO);
            return pageResult;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<FoodCategory> findAllFoodCategory(String shopId) {
        try{
            return this.foodCategoryDao.findAllFoodCategory(shopId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
}
